<template>
  <div class="login">
    <div class="company"><span>数据管理综合平台</span></div>
    <div class="login-panel-container">
      <div class="login-panel">
        <h1 class="title">请登录：</h1>

        <div class="form-container">
          <a-form
            ref="formRef"
            :rules="loginFormRules"
            :model="account"
            @finish="handleFinish"
            @finishFailed="handleFinishFailed"
            @validate="handleValidate"
          >
            <a-form-item label="账号" name="username">
              <a-input v-model:value="account.username"> </a-input>
            </a-form-item>

            <a-form-item label="密码" name="password">
              <a-input-password
                v-model:value="account.password"
                type="password"
                @keyup.enter="handleLoginClick"
              >
              </a-input-password>
            </a-form-item>
          </a-form>

          <div class="extend-panel">
            <div class="account-control">
              <a-checkbox v-model="isKeepPassword">记住密码</a-checkbox>
              <a href="#">忘记密码?</a>
            </div>

            <a-form-item>
              <a-button
                type="primary"
                class="login-btn"
                html-type="submit"
                @click="handleLoginClick"
                >登 录</a-button
              >
            </a-form-item>
          </div>
        </div>
      </div>
    </div>

    <div align="center">
      <span>Copyright @ 2011 - 2022 新东方智能化系统工程有限公司</span>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { loginFormRules } from './login-form-config'
import { localCache } from '@/utils/cache'
import { FormState } from './type'
import { FormInstance } from 'ant-design-vue'
import { useStore } from 'vuex'

export default defineComponent({
  setup() {
    const store = useStore()
    const formRef = ref<FormInstance>()
    const account = reactive<FormState>({
      username: localCache.getCache('username') ?? '',
      password: localCache.getCache('password') ?? ''
    })

    const isKeepPassword = ref(false)
    const handleLoginClick = () => {
      formRef.value
        ?.validate()
        .then((val) => store.dispatch('login/loginAction', val))
        .catch((err) => console.log('登录时验证表单出现异常', err))
    }

    const handleFinish = (val: FormState) => {
      console.log(val, account)
    }

    const handleFinishFailed = (err: any) => {
      console.log(err)
    }

    const handleValidate = (...args: any[]) => {
      console.log('')
    }

    return {
      formRef,
      loginFormRules,
      account,
      isKeepPassword,
      handleLoginClick,
      handleFinish,
      handleFinishFailed,
      handleValidate
    }
  }
})
</script>
<style scoped lang="less">
.login {
  width: 100%;
  height: 100%;
  background: url('../../assets/img/登录背景图片.png');
  background-repeat: no-repeat;
  // background-attachment: fixed;
  background-size: cover;
}

.company {
  width: 100%;
  height: 100px;
  color: #0083d0;
  font: 25px bold;

  display: table-cell;
  vertical-align: bottom;

  span {
    margin-left: 30px;
  }
}

.login-panel-container {
  width: 100%;
  height: 700px;
  // display: flex;
  // justify-content: flex-end;
}

.login-panel {
  width: 400px;
  height: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 10px;
  margin-top: 90px;

  display: inline-block;
  margin-left: 1000px;
}

.account-control {
  display: flex;
  justify-content: space-between;
}

.login-btn {
  width: 100%;
  margin-top: 10px;
}

.extend-panel {
  margin-left: 52.89px;
}

.form-container {
  margin-top: 50px;
}
</style>
